from http import HTTPStatus
from dashscope import Generation
import dashscope
from dashscope.api_entities.dashscope_response import Role

dashscope.api_key = ''  

def ask_question(question):
    gen = Generation()
    messages = []

    # 初始化对话
    if not messages:  
        systemRole = "审计人员"
        userRole = question
        messages = [{'role': 'system', 'content': systemRole},
                    {'role': 'user', 'content': userRole}]
    else:
        messages.append({'role': Role.USER, 'content': question})

    # 发送问题并获取回答
    response = gen.call(
        Generation.Models.qwen_turbo,
        messages=messages,
        result_format='message',  # 设置结果为消息格式
    )

    if response.status_code == HTTPStatus.OK:
        answer = response.output.choices[0]['message']['content']
        #print(answer)
        messages.append({'role': response.output.choices[0]['message']['role'],
                         'content': response.output.choices[0]['message']['content']})
        return answer
    else:
        error_message = 'Request id: %s, Status code: %s, error code: %s, error message: %s' % (
            response.request_id, response.status_code,
            response.code, response.message
        )
        print(error_message)
        return error_message

def interactive_session(question):
    while True:
        question = question
        if question == '':
            #print("对话结束。")
            return "你好。你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好"
        else:
            return ask_question(question)
